<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="付款状态" prop="paymentStatus">
        <el-select v-model="queryParams.paymentStatus" placeholder="全部" clearable>
          <el-option
            v-for="dict in dict.type.sys_payment_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="课程状态" prop="courseStatus">
        <el-select v-model="queryParams.courseStatus" placeholder="全部" clearable>
          <el-option
            v-for="dict in dict.type.sys_course_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户姓名" prop="nickName">
        <el-input v-model="queryParams.nickName" placeholder="请输入用户姓名" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="课程类型" prop="courseStatus">
        <el-select v-model="queryParams.courseType" placeholder="全部" clearable>
          <el-option
            v-for="dict in dict.type.sys_packaged_course"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:course:insert']"
        >充值课程</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="courseList" >
      <el-table-column label="序号" align="center" prop="courseId"  />
      <el-table-column label="课程类型" align="center" prop="courseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_packaged_course" :value="scope.row.courseType"/>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="nickName"  />
      <el-table-column label="付款金额" align="center" prop="paymentMoney" />
      <el-table-column label="课程状态" align="center" prop="courseStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_course_status" :value="scope.row.courseStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="付款状态" align="center" prop="paymentStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_payment_status" :value="scope.row.paymentStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="充值课程数 " align="center" prop="numberChange"/>
      <el-table-column label="剩余课程数" align="center" prop="currentNumber" />
      <el-table-column label="创建者" align="center" prop="createBy"  />
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleActivated(scope.row)"
            v-hasPermi="['system:course:activated']"
            v-if="scope.row.courseStatus === '0'"
          >激活</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:course:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:course:delete']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetails(scope.row)"
            v-hasPermi="['system:course:consume:list']"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户姓名">
              <el-select v-model="form.userId" placeholder="请选择用户姓名" @change="handleSelectChange" :disabled="disabledFlag">
                <el-option v-for="appUser in appUserList" :key="appUser.userId" :label="appUser.nickName" :value="appUser.userId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="充值节数" prop="numberChange">
              <el-input-number v-model="form.numberChange" placeholder="请输入充值课程节数" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="12">
              <el-form-item label="充值金额">
                <el-input-number v-model="form.paymentMoney" placeholder="请输入充值金额" :precision="2" :step="0.1"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="付款状态">
                <el-select v-model="form.paymentStatus" placeholder="请选择付款状态">
                  <el-option v-for="dict in dict.type.sys_payment_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主康复师">
              <el-input v-model="form.mainRehThe" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备用康复师" prop="spareRehThe">
              <el-input v-model="form.spareRehThe" disabled />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程类型">
              <el-select v-model="form.courseType" placeholder="请选择课程类型">
                <el-option v-for="dict in dict.type.sys_packaged_course" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listAppUser, getAppUser} from "@/api/app/user"
import {listCourser,getCourse,updateCourse,addCourse,deleteCourse,activatedCourse} from "@/api/system/course"


export default {
  name: "Course",
  dicts: ['sys_payment_status', 'sys_course_status','sys_packaged_course'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        paymentStatus: undefined,
        changeStatus: undefined,
        nickName: undefined,
        courseType: undefined
      },
      // 表单参数
      form: {},
      appUserList:[],
      courseList:[],
      disabledFlag:false
    };
  },
  created() {
    this.getList();
    this.getAppUserList();
  },
  methods: {
    /** 获取所有的用户列表 */
    getAppUserList () {
      listAppUser(null).then(response => {
        this.appUserList = response.rows;
      })
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listCourser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        courserId: undefined,
        userId: undefined,
        numberChange: undefined,
        paymentMoney: undefined,
        paymentstatus: undefined,
        mainRehThe: undefined,
        spareRehThe : undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "充值课程";
      this.disabledFlag = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const courserId = row.courseId
      getCourse(courserId).then(response => {
        this.form = response.data;
        this.handleSelectChange(this.form.userId);
        this.open = true;
        this.title = "修改充值课程";
        this.disabledFlag = true;
      });
    },

    handleActivated(row) {
      const courseId = row.courseId;
      activatedCourse(courseId).then(response => {
        this.$modal.msgSuccess("激活成功");
        this.getList();
      });
    },

    handleSelectChange(userId) {
      getAppUser(userId).then(response => {
        this.$set(this.form, 'mainRehThe', response.data.mainRehThe);
        this.$set(this.form, 'spareRehThe', response.data.spareRehThe);
      })
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const courseId = row.courseId;
      this.$modal.confirm('是否确认删除课程序号为"' + courseId + '"的数据项？').then(function() {
        return deleteCourse(courseId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    handleDetails(row) {
      this.$router.push('/system/course-data/index/' + row.courseId)
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.courseId != undefined) {
            updateCourse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
  }
};
</script>

